package ch.rheintalconnection.sysventory.technicalServices;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.*;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import ch.rheintalconnection.sysventory.technicalServices.wmi.WMIHostScanner;

public class ConnectionChecker implements Callable<WMIHostScanner> {
	
	private static Logger logger = LogManager.getLogger(ConnectionChecker.class);
	private String ipAddress;

	public ConnectionChecker(String ipAddress) {
		super();
		this.ipAddress = ipAddress;
	}

	@Override
	/*
	 * Returnt null, wenn keine Verbindung hergestellt werden konnte.
	 * @see java.util.concurrent.Callable#call()
	 */
	public WMIHostScanner call() throws Exception {
		
		WMIHostScanner hostScanner = null;
		
		try {
			Socket clientSocket = new Socket();
			
			SocketAddress addr = new InetSocketAddress(ipAddress, 135);
			clientSocket.connect(addr, 300);

			hostScanner = new WMIHostScanner(ipAddress);
			
			logger.info("Verbindung möglich: " + ipAddress);
			clientSocket.close();
		}
    	catch (com.jacob.com.ComFailException e) {
    		logger.info(e.getMessage() + " " + ipAddress);
    	}
    	catch (Exception e) {
			logger.info("Verbindung auf Socket nicht möglich: " + ipAddress);
		}
		
		return hostScanner;
	}

}
